package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GwContract;
import com.ruoyi.system.service.IGwContractService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 合同管理Controller
 *
 * @author ruoyi
 * @date 2025-04-23
 */
@Api("合同管理")
@RestController
@RequestMapping("/system/contract")
public class GwContractController extends BaseController {

    @Autowired
    private IGwContractService gwContractService;

    /**
     * 查询合同管理列表
     */
    @ApiOperation("查询合同管理列表 contractType: 公司治理、员工关系、合同管理")
//    @PreAuthorize("@ss.hasPermi('system:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(GwContract gwContract) {
        startPage();
        List<GwContract> list = gwContractService.selectGwContractList(gwContract);
        return getDataTable(list);
    }

    /**
     * 导出合同管理列表
     */
//    @PreAuthorize("@ss.hasPermi('system:contract:export')")
    @Log(title = "合同管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GwContract gwContract) {
        List<GwContract> list = gwContractService.selectGwContractList(gwContract);
        ExcelUtil<GwContract> util = new ExcelUtil<>(GwContract.class);
        util.exportExcel(response, list, "合同管理数据");
    }

    /**
     * 获取合同管理详细信息
     */
    @ApiOperation("获取合同管理详细信息")
//    @PreAuthorize("@ss.hasPermi('system:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(gwContractService.selectGwContractById(id));
    }

    /**
     * 新增合同管理
     */
    @ApiOperation("新增合同管理 contractType: 公司治理、员工关系、合同管理")
//    @PreAuthorize("@ss.hasPermi('system:contract:add')")
    @Log(title = "合同管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody GwContract gwContract) {
        return toAjax(gwContractService.insertGwContract(gwContract));
    }

    @ApiOperation("发起签署 contractType: 公司治理、员工关系、合同管理")
//    @PreAuthorize("@ss.hasPermi('system:contract:add')")
    @PostMapping("/addSign")
    public AjaxResult addSign(@Validated @RequestBody GwContract gwContract) throws Exception {
        return success(gwContractService.addSign(gwContract));
    }

    @ApiOperation("获取模板信息")
//    @PreAuthorize("@ss.hasPermi('system:contract:add')")
    @PostMapping("/getTemplateParam")
    public AjaxResult getTemplateParam(@RequestBody GwContract gwContract) throws Exception {
        return gwContractService.getTemplateParam(gwContract);
    }

    @ApiOperation("获取PDF图片")
    @PostMapping("/getPdfPics")
    public AjaxResult getPdfPics(@RequestBody GwContract gwContract) throws Exception {
        return gwContractService.getPdfPics(gwContract);
    }

    @ApiOperation("获取签署链接")
    @PostMapping("/getSignUrl")
    public AjaxResult getSignUrl(@RequestBody GwContract gwContract) {
        return gwContractService.getSignUrl(gwContract);
    }

    @ApiOperation("获取合同下载链接")
//    @PreAuthorize("@ss.hasPermi('system:contract:list')")
    @PostMapping("/getDownUrl")
    public AjaxResult getDownUrl(@RequestBody GwContract gwContract) throws Exception {
        return success(gwContractService.getDownUrl(gwContract));
    }

    @ApiOperation("撤销合同")
    @PostMapping("/revoke")
    public AjaxResult revoke(@RequestBody GwContract gwContract) {
        return gwContractService.revoke(gwContract);
    }

    /**
     * 修改合同管理
     */
    @ApiOperation("修改合同管理")
//    @PreAuthorize("@ss.hasPermi('system:contract:edit')")
    @Log(title = "合同管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody GwContract gwContract) {
        return toAjax(gwContractService.updateGwContract(gwContract));
    }

    /**
     * 删除合同管理
     */
    @ApiOperation("删除合同管理")
//    @PreAuthorize("@ss.hasPermi('system:contract:remove')")
    @Log(title = "合同管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(gwContractService.deleteGwContractByIds(ids));
    }

}
